package com.example.my_self_view.calendar

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.asynclayoutinflater.view.AsyncLayoutInflater
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.example.my_self_view.R
import com.example.my_self_view.databinding.ItemMonthBinding
import com.example.my_self_view.util.TimeUtil
import com.nlf.calendar.Solar
import com.nlf.calendar.util.HolidayUtil
import java.util.*


/**
 * @dsc     简介
 * @Author  LiWeiZhong
 * @Date    2025/2/28 15:32
 * @Version 1.0
 */
class CalendarAdapter: BaseAdapter<ItemCalendar, ItemMonthBinding>() {
    private val blankIds by lazy { arrayListOf<Int>(
        R.id.blank_1,R.id.blank_2,R.id.blank_3,
        R.id.blank_4,R.id.blank_5,R.id.blank_6
    ) }
//    private val dayIds by lazy { arrayListOf<Int>(
//        R.id.day_1,R.id.day_2,R.id.day_3,R.id.day_4,R.id.day_5,R.id.day_6,R.id.day_7,
//        R.id.day_8,R.id.day_9,R.id.day_10,R.id.day_11,R.id.day_12,R.id.day_13,R.id.day_14,
//        R.id.day_15,R.id.day_16,R.id.day_17,R.id.day_18,R.id.day_19,R.id.day_20,R.id.day_21,
//        R.id.day_22,R.id.day_23,R.id.day_24,R.id.day_25,R.id.day_26,R.id.day_27,R.id.day_28,
//        R.id.day_29,R.id.day_30,R.id.day_31
//    ) }

    override fun getB(inflater: LayoutInflater, parent: ViewGroup): ItemMonthBinding {
        return ItemMonthBinding.inflate(inflater, parent, false)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        super.onCreateViewHolder(parent, viewType)
        val asyncInflater = AsyncLayoutInflater(parent.context)
        asyncInflater.inflate(R.layout.item_month, parent) { view, resId, parent1 ->
            // 视图加载完成后，使用 ViewBinding 进行视图绑定
            val binding = ItemMonthBinding.bind(view)
            // 创建 ViewHolder 并将绑定传递给它
            ViewHolder(binding)
        }

        // 返回 null，因为视图会在回调中处理
        val inflater = LayoutInflater.from(parent.context)
        return ViewHolder(ItemMonthBinding.inflate(inflater,parent, false))
    }


    override fun addItemListener(holder: RecyclerView.ViewHolder) {

    }

    @SuppressLint("SetTextI18n")
    override fun bindHolder(holder: ViewHolder, position: Int) {
        // 获取root、context
        val root = holder.mBinding.root
        val context = root.context
        getData(position).apply {
            // 获取当前calendar
            val calendar = Calendar.getInstance()
            calendar.set(Calendar.YEAR, year)
            calendar.set(Calendar.MONTH, month - 1)
            calendar.set(Calendar.DAY_OF_MONTH, 1)
            val firstWeekDay = calendar.get(Calendar.DAY_OF_WEEK)
            val maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
            // 补充前面的
            val needBlank = if (firstWeekDay == 1) 6 else firstWeekDay - 2
            for (i in 0 until needBlank) {
                val view = root.findViewById<TextView>(blankIds[i])
                view.visibility = View.VISIBLE
            }
            for (i in needBlank until 6) {
                val view = root.findViewById<TextView>(blankIds[i])
                view.visibility = View.GONE
            }
            // 填充后面的
//            for (day in 1..maxDay) {
//                val view = root.findViewById<RelativeLayout>(dayIds[day - 1])
//                view.visibility = View.VISIBLE
//                view.apply {
//                    // 设置日期
//                    val tvNumber = view.findViewById<TextView>(R.id.tv_number)
//                    tvNumber.text = day.toString()
//                    // 设置聚焦监听
//                    setOnFocusChangeListener { v, hasFocus ->
//                        val focusNumber = v.findViewById<TextView>(R.id.tv_number)
//                        val focusFestival = v.findViewById<TextView>(R.id.tv_festival)
//                        val focusDate = focusNumber.tag as String
//                        val focusToday = TimeUtil.TIME_FORMAT("yyyy-MM-dd", Calendar.getInstance().time.time)
//                        if (focusDate == focusToday) {
//                            if (hasFocus) {
//                                focusNumber.setTextColor(ContextCompat.getColor(context, R.color.white))
//                                focusFestival.setTextColor(ContextCompat.getColor(context, R.color.white))
//                            } else {
//                                focusNumber.setTextColor(ContextCompat.getColor(context, R.color.blue))
//                                focusFestival.setTextColor(ContextCompat.getColor(context, R.color.grey))
//                            }
//                        } else {
//                            focusNumber.setTextColor(ContextCompat.getColor(context, R.color.black))
//                            focusFestival.setTextColor(ContextCompat.getColor(context, R.color.grey))
//                        }
//                    }
//                    // 是否是今天，若是今天，字体颜色变成蓝色，否则原本颜色
//                    calendar.set(Calendar.DAY_OF_MONTH, day)
//                    val date= TimeUtil.TIME_FORMAT("yyyy-MM-dd", calendar.time.time)
//                    tvNumber.tag = date
//                    if (date== today) {
//                        tvNumber.setTextColor(ContextCompat.getColor(context, R.color.selector_focus_blue_white))
//                        background = ContextCompat.getDrawable(context, R.drawable.bg_today)
//                        requestFocus()
//                    }
//                    // 设置农历与节日
//                    val lunar = Solar.fromYmd(year, month, day).lunar
//                    val holiday = HolidayUtil.getHoliday(year, month, day)
//                    val tvFestival = view.findViewById<TextView>(R.id.tv_festival)
//                    if (holiday != null && holiday.day == holiday.target) {
//                        tvFestival.text = holiday.name
//                    } else {
//                        val festivals = lunar.festivals
//                        if (festivals.size > 0) tvFestival.text = festivals[0]
//                        else {
//                            if (lunar.dayInChinese == "初一") tvFestival.text = lunar.monthInChinese + "月"
//                            else tvFestival.text = lunar.dayInChinese
//                        }
//                    }
//                }
//            }
//            for (day in maxDay..31) {
//                val view = root.findViewById<RelativeLayout>(dayIds[day - 1])
//                view.visibility = View.GONE
//            }
        }
    }
}